﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Xml.Linq;


namespace 花店管理系统.geren
{
    public partial class Increase_gongao : Form
    {
        public Increase_gongao()
        {
            InitializeComponent();
        }
        SqlDataAdapter daGongao;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from T_gongao";
            daGongao = new SqlDataAdapter(str, DB.cn);
            daGongao.Fill(ds, "gongao_info");
            DB.cn.Close();
        }
        void showAll()
        {
            DataView dvdaGongao = new DataView(ds.Tables["gongao_info"]);
            dgvIncrease.DataSource = dvdaGongao;
            dgvIncrease.Columns[3].Width = 500;
        }
        void dgvHead()
        {
            this.dgvIncrease.Columns[0].HeaderText = "编号";
            this.dgvIncrease.Columns[1].HeaderText = "标题";
            this.dgvIncrease.Columns[2].HeaderText = "内容";
            this.dgvIncrease.Columns[3].HeaderText = "时间";

        }

        private void Increase_gongao_Load(object sender, EventArgs e)
        {
            init();
            showAll();
            dgvHead();
        }

        private void btnIncrease_Click(object sender, EventArgs e)
        {
            if (tbId.Text == "" )
            {
                MessageBox.Show("编号不能为空");

            }
            else
            {
                DB.GetCn();
                DialogResult dr = MessageBox.Show("你确定要添加吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    CheckGongaoIdExists();
                }
                else
                {
                    return;
                }
                //使用存储过程添加日记
                SqlCommand cmd = new SqlCommand("add_log", DB.cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("tname", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("L_type", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("L_date", SqlDbType.DateTime));
                cmd.Parameters.Add(new SqlParameter("L_table", SqlDbType.NVarChar));

                cmd.Parameters["tname"].Value = Login.username;
                cmd.Parameters["L_type"].Value = "添加";
                cmd.Parameters["L_date"].Value = DateTime.Now;
                cmd.Parameters["L_table"].Value = "gongao表";

                try
                {
                    SqlCommandBuilder dbGongao = new SqlCommandBuilder(daGongao);
                    daGongao.Update(ds, "gongao_info");
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            void CheckGongaoIdExists()
            {
                string s_id = "select * from T_gongao where gongao_id='" + tbId.Text + "'";
                DataTable dtl = DB.GetDataSet(s_id);
                if (dtl.Rows.Count > 0)
                {
                    MessageBox.Show("此编号已存在，请重新输入");
                }
                else
                {
                    DataRow DepRow = ds.Tables["gongao_info"].NewRow();
                    DepRow["gongao_id"] = tbId.Text.Trim();
                    DepRow["biaoti"] = tbBiaoti.Text.Trim();
                    DepRow["neirong"] = tbneirong.Text.Trim();
                    ds.Tables["gongao_info"].Rows.Add(DepRow);


                }
            }
        }

        private void btnCanle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
